﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Admin2020.Data.Data
{
    public interface IRepository<T>
    {
        /// <summary>
        /// 根据Id返回实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        T GetById(object id);

        /// <summary>
        /// 根据实体插入记录
        /// </summary>
        /// <param name="entity"></param>
        void Insert(T entity);

        /// <summary>
        /// 根据插入记录（异步）
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        Task InsertAsync(T entity);

        /// <summary>
        /// 批量插入记录
        /// </summary>
        /// <param name="entities"></param>
        void Insert(IEnumerable<T> entities);

        /// <summary>
        /// 批量插入记录（异步）
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        Task InsertAsync(IEnumerable<T> entities);

        /// <summary>
        /// 更新实体对应的记录
        /// </summary>
        /// <param name="entity"></param>
        void Update(T entity);

        /// <summary>
        /// 批量更新实体对应的记录
        /// </summary>
        /// <param name="entities"></param>
        void Update(IEnumerable<T> entities);

        /// <summary>
        /// 根据实体删除指定记录
        /// </summary>
        /// <param name="entity"></param>
        void Delete(T entity);

        /// <summary>
        /// 根据给定的多个实体批量删除对应的记录
        /// </summary>
        /// <param name="entities"></param>
        void Delete(IEnumerable<T> entities);

        /// <summary>
        /// 获取一张表
        /// </summary>
        IQueryable<T> Table { get; }

        /// <summary>
        /// 获取一张表（无懒加载模式）
        /// </summary>
        IQueryable<T> TableNoTracking { get; }
        
        /// <summary>
        /// 执行一条格式化了参数的sql语句
        /// </summary>
        /// <param name="sql">sql语句字符串</param>
        /// <returns></returns>
        IQueryable<T> FromSqlInterpolated(FormattableString sql);

        /// <summary>
        /// 执行一条sql语句，并使用插值的方式传入参数
        /// </summary>
        /// <param name="sql">sql语句字符串</param>
        /// <param name="paramerers">参数数组</param>
        /// <returns></returns>
        IQueryable<T> FromSqlRaw(string sql, params object[] paramerers);
    }
}
